# This is appended to packaging/deb/amanda-backup-*.postinst
# vim: ft=sh

# Note: this script needs to do the same things in all maintainer
# script cases: configure, abort-remove, abort-upgrade.

LOGFILE=`mktemp /tmp/amanda-postinst.log.XXXXXXXXXXX`
if [ $? -ne 0 ]; then
	echo "Unable to create log file!"
	exit 1
fi
amanda_user=amandabackup
amanda_group=disk
AMANDAHOMEDIR=%%AMANDAHOMEDIR%%
os=Linux
dist=%%DISTRO%%
LOGDIR=%%LOGDIR%%
INSTALL_LOG="${LOGDIR}/install.log"
SYSCONFDIR=/etc
SBINDIR=/usr/sbin
AMTMP="/tmp/amanda"

AMANDATES=${SYSCONFDIR}/amandates
encoder=`{ command -v base64 2>/dev/null; } || { command -v uuencode 2>/dev/null; }`

# ---------- Common functions ------------
%%COMMON_FUNCTIONS%%
%%POST_INST_FUNCTIONS%%

# -------- End Common functions ----------

check_xinetd "amanda${pkg_type}"
case $? in
	0) backup_xinetd "amanda${pkg_type}"
	   install_xinetd "amanda${pkg_type}"
	;;
	1) install_xinetd "amanda${pkg_type}" ;;
	2) logger "Xinetd config not installed: either xinetd config is not present or xinetd.d is a file." ;;
	*) logger "bad return from check_xinetd"
	   cat ${LOGFILE} >> ${INSTALL_LOG}
	   exit 1
	;;
esac

# amanda${pkg_type} should not have the amanda${other_pkg_type} xinetd installed.
check_xinetd "amanda${other_pkg_type}"
case $? in
	0) backup_xinetd "amanda${other_pkg_type}" ;;
esac

check_superserver_running "xinetd"
[ "$?" = "0" ] && action=restart || action=start
reload_xinetd $action
create_amandates
check_amandates
create_ampassphrase || \
    logger "Info: amcryptsimple and amcrpyt will not work until .am_passphrase is created"
create_gnupg
create_amkey || \
    logger "Info: amcrypt will not work until keys are created."
# Checks permissions, but only tries decrypting if both .am_passphrase
# and .gnupg/am_key.gpg exist.
check_gnupg
create_amandahosts
check_amandahosts_entry root amindexd amidxtaped
check_amandahosts_entry ${amanda_user} amdump
check_amandahosts_perms
create_ssh_key ${pkg_type}
if [ "${pkg_type}" = "server" ]; then
    create_ssh_key ${other_pkg_type}
fi
create_profile
check_profile
install_client_conf
create_amtmp

logger "Amanda ${pkg_type} installation complete."
cat $LOGFILE >> $INSTALL_LOG && {
    rm $LOGFILE;
    echo "Amanda installation log can be found in '${INSTALL_LOG}'.";
} || \
    echo "Amanda installation log can be found in '${LOGFILE}'.";

